package com.lenovo.lcdm.team.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.lenovo.lcdm.team.entity.TeamGroup;
import com.lenovo.lcdm.team.entity.TeamGroupRole;
import com.lenovo.lcdm.team.mapper.TeamGroupRoleMapper;
import com.lenovo.lcdm.team.service.ITeamGroupRoleService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;
import java.util.stream.Collectors;

/**
 * Group-Role Service
 * @author xujing30
 * @since 2023-12-22
 */
@Service
public class TeamGroupRoleServiceImpl extends ServiceImpl<TeamGroupRoleMapper, TeamGroupRole> implements ITeamGroupRoleService {

    @Override
    public void saveGroupRoles(TeamGroup group, List<Long> roleIds){
        LambdaQueryWrapper<TeamGroupRole> qw = new LambdaQueryWrapper<>();
        qw.eq(TeamGroupRole::getGroupId, group.getId());
        this.remove(qw);
        List<TeamGroupRole> groupRoles = roleIds.stream().map(item->{
            TeamGroupRole groupRole = new TeamGroupRole();
            groupRole.setGroupId(group.getId());
            groupRole.setRoleId(item);
            if(group.getModifier() != null) {
                groupRole.setCreator(group.getModifier());
                groupRole.setModifier(group.getModifier());
            }
            groupRole.setCreateTime(LocalDateTime.now());
            groupRole.setModifyTime(LocalDateTime.now());
            return groupRole;
        }).collect(Collectors.toList());
        this.saveBatch(groupRoles);
    }

}
